<template>
  <div class="login">
    <div class="form">
      <div class="title">
        智慧点餐平台
      </div>
      <el-form :model="formData" :rules="rules" ref="loginForm">
        <el-form-item prop="account">
          <el-input v-model="formData.account" placeholder="请输入账号" :prefix-icon="UserFilled"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="formData.password" show-password placeholder="请输入密码" :prefix-icon="Lock"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login" style="width: 100%;">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { reactive } from 'vue'
import { Lock, UserFilled } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { checkLogin } from '@/api/account'
import {useUserStore } from '@/stores/userStore'

const store = useUserStore ()

const formData = reactive({
  account: '',
  password: ''
})
const rules = reactive({
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 1, max: 6, message: '账号长度必须为1到6位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})
const loginForm = ref(null) // 添加表单引用
const router = useRouter()

// 登录函数
const login = () => {
  // 调用表单验证
  loginForm.value.validate((valid) => {
    if (valid) {
      // 如果验证通过，调用接口
      checkLogin(formData)
        .then((res) => {
          console.log(res);
          if (res.code === 0) { // 判断接口返回的 code 是否为 0

            // 将用户登录信息存储到 pinia中
            // store.changeUserInfo({
            //   id: res.data.id,
            //   role: res.data.role,
            //   token: res.data.token
            // });
            // console.log(store.userInfo)

            //先放sessionStorage，跑通
            sessionStorage.setItem('user', JSON.stringify({
              id: res.id,
              role: res.role,
              token: res.token
            }));

            ElMessage({
              message: '登录成功了',
              type: 'success'
            });
            router.push('/home'); // 登录成功，跳转到 /home 页面
          } else {
            ElMessage({
              message: '登录失败，账号或密码错误', // 如果 code 不为 0，提示登录失败
              type: 'error'
            });
          }
        })
        .catch((err) => {
          console.log(err);
          ElMessage({
            message: '登录失败，服务器错误', // 捕获接口调用的错误
            type: 'error'
          });
        });
    } else {
      // 如果验证失败，提示用户
      ElMessage({
        message: '表单验证失败，请检查输入',
        type: 'error'
      });
    }
  });
};
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  align-items: center;
  justify-content: center;
}
.form {
  width: 400px;
  height: 400px;
}

.title {
  width: 100%;
  height: 50px;
  color: #fff;
  font-size: 30px;
  text-align: center;
  line-height: 50px;
  margin-bottom: 20px;
}
:deep(.el-input__wrapper) {
  background-color: rgb(0, 0, 0, 0);
}
:deep(.el-input__inner) {
  color: #fff;
}
:deep(.el-input) {
  margin-bottom: 30px;
  height: 40px;
}
</style>